package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Team;
import com.kkb.service.TeamService;
import com.kkb.vo.QueryVo;
import com.kkb.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/team")
@ResponseBody
public class TeamController {
    @Resource
    private TeamService teamService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultVo<Team> queryByPage(Integer pageNum, Integer pageSize, QueryVo vo){
        if (pageNum==null || pageNum<=0){
            pageNum = 1;
        }
        if (pageSize==null || pageSize<=0){
            pageSize = 5;
        }
        PageInfo<Team> pageInfo = teamService.queryByPage(pageNum, pageSize, vo);
        return new ResultVo<>(pageInfo);

    }

    /**
     * 添加
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVo<Team> addTeam(Team  team){
        System.out.println(team);
        int i = teamService.addTeam(team);
        if (i==1){
            return new ResultVo<Team>();
        }
        return new ResultVo<>(500,"服务器内部出错！");
    }

    /**
     * 根据主键去查询一个team
     */
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVo<Team> queryById(@PathVariable("id") Integer teamId){
        Team team = teamService.queryById(teamId);
        return new ResultVo<Team>(team);
    }

    /**
     * 根据主键去更新一个team
     */
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultVo<Team> updateTeam(@PathVariable("id") Integer teamId,Team team){
        System.out.println(team);
        team.setTeamId(teamId);
        int i = teamService.updateTeam(team);
        if (i==1){
            return new ResultVo<>();
        }
        return new ResultVo<>(500,"服务器内部出现错误");
    }
    /**
     * 根据主键去删除
     */
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVo<Team> deleteTeam(@PathVariable("id")Integer teamId){
        int i = teamService.deleteTeam(teamId);
        if (i==1){
            return new ResultVo<>();
        }
        return new ResultVo<>(500,"服务器内部出现错误");
    }

    /**
     * 文件上传
     */
    @RequestMapping("{id}")
    public ResultVo<Team> uploadLogo(@RequestParam("logo") MultipartFile myFile, HttpServletRequest req
    , @PathVariable("id") Integer teamId){
        //文件上传到指定服务器
        String realPath = req.getServletContext().getRealPath("/img/uploadFile/");
        //获取原始文件的名称
        String originalFilename = myFile.getOriginalFilename();
        //获取文件名称.的下标
        int index = originalFilename.lastIndexOf(".");
        //获取原始文件的后缀名
        String  hz = originalFilename.substring(index);
        //获取新名称的前缀
        String randomName = UUID.randomUUID().toString().replace("-","");
        //新的文件名称为:
        String loginName = randomName+hz;
        //开始上传
        int num = 0;
        try {
            myFile.transferTo(new File(realPath+"/"+loginName));
            System.out.println("上传成功！"+realPath+"/"+loginName);
            //将图片的名称更新到数据库中
            Team team = new Team();
            team.setTeamId(teamId);
            team.setTeamLogo(loginName);
            num = teamService.updateTeam(team);
            if (num==1){
                return new ResultVo<>();
            }else{
                return new ResultVo<>(500,"服务器内部出现错误");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVo<>(500,"图片上传异常"+e.getMessage());
        }

    }

}
